/**
 * Copyright 2010 Kurtis Nusbaum
 *
 * This file is part of RemoteRunner.  
 *
 * RemoteRunner is free software: you can 
 * redistribute it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation, either version 2 of the 
 * License, or (at your option) any later version.  
 *
 * RemoteRunner is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.  You should have received a copy of the GNU  General 
 * Public License along with RemoteRunner. If not, see 
 * http://www.gnu.org/licenses/.
 */

package org.klnusbaum.remoterunner;

import android.util.Log;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.app.AlertDialog;
import android.text.TextWatcher;
import android.text.Editable;
import android.content.DialogInterface;
import android.content.Context;


/**
 * Class used for setting the password of a host.
 */
public class SetPasswordDialog extends AlertDialog implements TextWatcher{

	/** Tag used for logging purposes. */
	private static final String TAG = "SetPassword";
	/** EditText using for specifying a password. */
  private EditText passwordEdit;

	/** Listner used for when the ok and cancel button are clicked. */
	private final DialogInterface.OnClickListener	buttonListener;

	static final int BUTTON_OK = DialogInterface.BUTTON_POSITIVE;

	/** Constants used for helping save state. */
	private static final String EXTRA_PASSWORD = "PASSWORD";
	private static final String EXTRA_MY_STATE = "MY_STATE";
	
	/**
	 * Constructs a SetPasswordDialog.
   *
   * @param context The context in which the dialog is being created.
   * @param buttonListener The listener used for the ok and cancel buttons.
	 */
	public SetPasswordDialog(Context context, OnClickListener buttonListener,
		DialogInterface.OnDismissListener onDismissListener){
		super(context);
		setOnDismissListener(onDismissListener);
		this.buttonListener = buttonListener;
	}

	@Override
  public void onCreate(Bundle savedInstanceState){
		View view = getLayoutInflater().inflate(R.layout.set_password_layout, null);
		setView(view);
    setTitle(R.string.set_password);
		
    passwordEdit = (EditText) view.findViewById(R.id.passwordEdit);
		passwordEdit.addTextChangedListener(this);

		setButton(
			BUTTON_OK, 
			getContext().getString(android.R.string.ok),
			buttonListener);
		setButton(
			BUTTON_NEGATIVE, 
			getContext().getString(android.R.string.cancel), 
			buttonListener);
    super.onCreate(savedInstanceState);
		validate();
  }

	@Override
	public Bundle onSaveInstanceState(){
		Bundle toReturn = super.onSaveInstanceState();
		Bundle state = new Bundle();
		state.putCharSequence(EXTRA_PASSWORD, passwordEdit.getText());
		toReturn.putBundle(EXTRA_MY_STATE, state);
		return toReturn;
	}	

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState){
		super.onRestoreInstanceState(savedInstanceState);
		Bundle state = savedInstanceState.getBundle(EXTRA_MY_STATE);
		passwordEdit.setText(state.getCharSequence(EXTRA_PASSWORD));
		validate();
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count){
	
	}

	@Override
	public void beforeTextChanged(
		CharSequence s, int start, int count, int after)
	{
	
	}

	@Override
	public void afterTextChanged(Editable editable){
		validate();
	}

	/** 
	 * Validates whether or not the current inputs the user has specied 
   * are valid. If they aren't the Ok button is disabled.
   */
	private void validate(){
		if(!passwordEdit.getText().toString().equals("")){
			getButton(BUTTON_OK).setEnabled(true);
		}
		else{
			getButton(BUTTON_OK).setEnabled(false);
		}
	}	

	/**
 	 * Gets what the user has specified as the password.
   *
   * @return What the user has currently specified the password as.
   */
	public String getPassword(){
		return passwordEdit.getText().toString();
	}
}
